﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CL.Business.BLL;
using CL.Business.Entity;
using CL.Tools.Common;
using CL.Enum.Common.Union;

namespace CL.UnionWeb.union
{
    public class BasePage : System.Web.UI.Page
    {
        public BasePage()
        {
            this.Load += new EventHandler(UnionPage_Load);
        }

        private void UnionPage_Load(object sender, EventArgs e)
        {
            //判断管理员是否登录
            if (!IsAdminLogin())
            {
                Response.Write("<script>alert(\"" + "请登录!" + "\");location.href='/Login.aspx'</script>");
                Response.End();
            }
        }
        /// <summary>
        /// 判断推广联盟会员是否已经登录(解决Session超时问题)
        /// </summary>
        protected bool IsAdminLogin()
        {
            //如果Session为Null
            if (Session[CLKeys.SESSION_ADMIN_INFO] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string UnionName = Utils.GetCookie("UserName", "CLAdmin");
                string UnionPwd = Utils.GetCookie("UserPwd", "CLAdmin");
                if (UnionName != "" && UnionPwd != "")
                {
                    var model = new AccountBLL().QueryEntity(UnionName);
                    if (model != null && model.AccountPwd == UnionPwd)
                    {
                        Session[CLKeys.SESSION_ADMIN_INFO] = model;
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 取得管理员信息
        /// </summary>
        public AccountEntity GetUnionInfo()
        {
            if (IsAdminLogin())
            {
                AccountEntity model = Session[CLKeys.SESSION_ADMIN_INFO] as AccountEntity;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }

        protected bool ShowTip()
        {
            var un_Entity = GetUnionInfo();
            if ((int)AccountStatusEnum.certification == un_Entity.AccountStatus ||
                (int)AccountStatusEnum.toBeAudited == un_Entity.AccountStatus ||
                (int)AccountStatusEnum.noPass == un_Entity.AccountStatus)
                return true;
            else
                return false;
        }

        protected void SendMsgToView(string msg, string url = null)
        {
            if (string.IsNullOrEmpty(url))
                Response.Write("<script >alert(\"" + msg + "\");</script>");
            else
                Response.Write("<script >alert(\"" + msg + "\");location.href='" + url + "';</script>");
            Response.End();
        }
    }
}